<?php

/**
 * basic house calendarLooks view model
 * @author perry
 */

namespace Home\Model;
use Think\Model\ViewModel;

class BHCalendarViewModel extends ViewModel {
	private $CalendarAlias='BHC';
	private $LooksAlias="BHL";

	protected function _initialize(){
		$this->viewFields = array(     
    		$this->CalendarAlias=>array('id','uid','year','month','day','look_id','is_top','memo'=>'calendar_memo'
    			,'_type'=>'LEFT'
    			,'_table'=>'eosi_bh_calendar'),     
    		$this->LooksAlias=>array('cate','label','img','item_ids','name','memo'=>'look_memo','create_time'=>'look_create_time'
    			, '_on'=>"{$this->CalendarAlias}.look_id={$this->LooksAlias}.id"
    			,'_table'=>'eosi_bh_looks'),      
    	); 
    	$this->looksM=D('Home/BHLooks');
	}

//前台用户根据指定年月获得该月自己的所有日历中造型，如果某日官方灵感造型有推荐，则官方推荐也获得
   public function getMonthCalendar($year,$month,$uid){
   		$super_style_uid=C('super_style_uid');
        $map=array('year'=>$year,'month'=>$month);
        $super_style_uid_str=implode(",", $super_style_uid);
        $map['_string'] = " ($this->CalendarAlias.uid = $uid)  OR ( $this->CalendarAlias.uid in ($super_style_uid_str) )";
        $re=$this->where($map)->order("day asc,{$this->LooksAlias}.cate asc, {$this->CalendarAlias}.create_time desc")->select();
        //数据表原始数据格式整理成前端需要的数据格式
        $fre=array();
        foreach ($re as &$value) {
        	$day=$value['day'];
        	if(!array_key_exists($day, $fre)){
        		$fre[$day]=array();
        	}
            //unset掉不需要返回到前端的数据
        	// unset($value['look_id']);
        	// unset($value['uid']);
        	unset($value['year']);
        	unset($value['month']);
        	unset($value['day']);
        	unset($value['label']);
        	unset($value['item_ids']);
        	unset($value['name']);
        	unset($value['look_memo']);
            $value['img']=ic_getUploadImgFullPath('looks',$value['img'],$value['look_create_time']);
            unset($value['look_create_time']);
        	$fre[$day][]=$value;

        }
        //以下循环作用：如果某日用户自己无1造型置顶，而官方推荐有，则将官方推荐中的第一条置顶
        foreach ($fre as $day => $value) {
        	$findUserTop=-1;
        	$super_style_key=-1;
        	foreach ($value as $key => $look) {
        		if($look['uid']==$uid && $look['is_top']==1){
        			$findUserTop=$key;
        			break;
        		}
        		if(in_array($look['uid'], $super_style_uid) && $super_style_key==-1){
        			$super_style_key=$key;
        		}
        	}

            //以下代码为了龙飞使用接口数据方便，他要求不管用户手动置顶的还是官方推荐的，都放到每日返回数组第一位
        	if($findUserTop==-1){//该日用户自己无造型置顶
        		if($super_style_key!=-1){ //而官方推荐有，则将官方推荐中的第一条置顶
	        		$fre[$day][$super_style_key]['is_top']=1;
                    //龙飞要求把置顶的放到最前面
                    $temp=$fre[$day][$super_style_key];
                    unset($fre[$day][$super_style_key]);
                    array_unshift($fre[$day], $temp);
                    //dump('官方推荐放到最前面置顶');
	        	}
        	}
            else {
                if($findUserTop!=0){
                    //龙飞要求把置顶的放到最前面
                    $temp=$fre[$day][$findUserTop];
                    unset($fre[$day][$findUserTop]);
                    array_unshift($fre[$day], $temp);
                    // dump("用户自己的置顶放到数组最前面");
                }
            }
        }
        return $fre;
        
   }
}

